package com.zlll.winner.controller.weixin;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.service.weixin.IWxToolsService;
import com.zlll.winner.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "/wx/statistics")
public class WxStatisticalController {


    @Reference
    private IWxToolsService wxToolsService;
    /**
     * 月留存
     * @param params
     * @return
     */
    @PostMapping(value = "getMonthlyRetain")
    public Object getMonthlyRetain(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }

    /**
     * 周留存
     * @param params
     * @return
     */
    @PostMapping(value = "getWeeklyRetain")
    public Object getWeeklyRetain(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }

    /**
     * 日留存
     * @param params
     * @return
     */
    @PostMapping(value = "getDailyRetain")
    public Object getDailyRetain(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }

    /**
     * 用户访问小程序数据月趋势
     * @param params
     * @return
     */
    @PostMapping(value = "getMonthlyVisitTrend")
    public Object getMonthlyVisitTrend(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }

    /**
     * 用户访问小程序数据周趋势
     * @param params
     * @return
     */
    @PostMapping(value = "getWeeklyVisitTrend")
    public Object getWeeklyVisitTrend(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }

    /**
     * 用户访问小程序数据日趋势
     * @param params
     * @return
     */
    @PostMapping(value = "getDailyVisitTrend")
    public Object getDailyVisitTrend(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }


    /**
     * 获取小程序新增或活跃用户的画像分布数据。时间范围支持昨天、最近7天、最近30天。其中，新增用户数为时间范围内首次访问小程序的去重用户数，活跃用户数为时间范围内访问过小程序的去重用户数。
     * @param params
     * @return
     */
    @PostMapping(value = "getUserPortrait")
    public Object getUserPortrait(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }


    /**
     * 获取用户小程序访问分布数据。
     * @param params
     * @return
     */
    @PostMapping(value = "getVisitDistribution")
    public Object getVisitDistribution(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }


    /**
     * 访问页面。目前只提供按 page_visit_pv 排序的 top200。。
     * @param params
     * @return
     */
    @PostMapping(value = "getVisitPage")
    public Object getVisitPage(@RequestBody JSONObject params){
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage?access_token=");
        url.append(wxToolsService.getAccessToken(false));
        String resp = HttpClientUtil.doPostJson(url.toString(),params.toJSONString());
        return JSON.parseObject(resp);
    }



}
